package com.zjr.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.zjr.dao.LogDao;
import com.zjr.domain.Log;
import com.zjr.domain.User;
import com.zjr.utils.ViewUser;
/**
 * 日志DAO层实现类
 * */
public class LogDaoImpl extends HibernateDaoSupport implements LogDao {
	
	//添加日志实现方法
	@Override
	public boolean save(Log log) {
		Serializable result = null;
		try {
			 result = this.getHibernateTemplate().save(log);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (Integer)result > 0;
	}

	@Override
	public List<Log> find(User user) {
		if(user.getRole() == 2 || user.getRole() == 3 ){
			if(this.getHibernateTemplate().find("from Log") != null){
				return (List<Log>) this.getHibernateTemplate().find("from Log");
			}
		}else if(user.getRole() == 1){
			String hql = "from Log where state=0 or state=1";
			if(this.getHibernateTemplate().find(hql) != null){
				return (List<Log>) this.getHibernateTemplate().find(hql);
			}
		}else{
			String hql = "from Log where uid=?";
			if(this.getHibernateTemplate().find(hql, user.getUid()) != null){
				return (List<Log>) this.getHibernateTemplate().find(hql, user.getUid());
			}
		}
		return null;
	}

	@Override
	public Log findById(int lid) {
		return this.getHibernateTemplate().get(Log.class, lid);
	}

	@Override
	public void modify(Log log) {
		this.getHibernateTemplate().update(log);
	}

	@Override
	public void delete(int lid) {
		this.getHibernateTemplate().delete
			(this.getHibernateTemplate().get(Log.class, lid));
	}
	
}
